--
-- Author: yawei.zhu
-- Date: 2018-05-09 11:08:14
--
-- GoldTaskRender
--

local IconRender = require("app.widget.renders.IconRender")

local ClassRef = app.mvc.createRenderClass(...)

--data =
function ClassRef.createWithData(data, node)
    local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.TaskBountyItemNode))
    if node == nil then
        ccui.Helper:doLayout(sprite)
    end
    sprite:init(data)
    return sprite
end
function ClassRef.create(node)
    return ClassRef.createWithData(nil, node)
end

--
function ClassRef:init(data)
    self.super.init(self, data)

    self:setContentSize(self._ui["Panel"]:getContentSize())

    self:refresh()
end

function ClassRef:setIndex(index)
    self._index = index
end

function ClassRef:setDropItemNode(root, dropItem)
    local panelRoot = root:getChildByName("Panel")
    if dropItem then
        root:setVisible(true)
        local uiItem = IconRender.create(root)
        uiItem:setData(dropItem)
        uiItem:setTipEnable(true)
    else
        root:setVisible(false)
    end
end

function ClassRef:refresh()
    if self._data == nil then return end
    local task = self._data

    local fameSubId = executeFunc(self._onClick, "getSelFameSubId")

    local bgBtn = self._ui["Panel/bg"]
    bgBtn:setZoomScale(0)
    bgBtn:setTouchEnabled(false)

    local Panel_img = self._ui.Panel_img
    local Panel_icon = self._ui.Panel_icon
    local taskNameNode = Panel_img:getChildByName("Text_tlt")
    local taskDesNode = Panel_img:getChildByName("Text")
    local grade = Panel_img:getChildByName("grade")
    local taskIconNode = Panel_img:getChildByName("img")
    local quality = tonumber(task:getConfig().quality) or 1
    grade:setColor(Enums.QualityColor1[quality] or Enums.QualityColor[quality])

    taskNameNode:setString(L(task:getConfig().name))
    taskDesNode:setString(L(task:getConfig().target))

    task:setTaskIcon(taskIconNode)

    local dLen = #task.dropItems
    dLen = (dLen == 0) and 3 or dLen
    local uWidth = Panel_icon:getContentSize().width
    for i = 1,3 do
        local name = "icon_" .. i
        local iconNode = Panel_icon:getChildByName(name)
        local iconRoot = iconNode:getChildByName("Panel")

        local sWidth = iconRoot:getContentSize().width * iconRoot:getParent():getScale() + 10
        local sx = (uWidth - dLen * sWidth) / 2 + (i - 1) * sWidth + 5
        iconNode:setPosition(cc.p(sx,iconNode:getPositionY()))

        local dropItem = task.dropItems[i]
        self:setDropItemNode(iconNode,dropItem)
    end

    local btnRefresh = self._ui.btn_refresh
    local btnGet = self._ui.btn_receive
    local btnReward = self._ui.btn_reward

    local trackPanel = self._ui.Panel_track
    local rootBar = trackPanel:getChildByName("bar")
    local progressBar = rootBar:getChildByName("LoadingBar")
    local progressText = rootBar:getChildByName("BitmapFontLabel_5")
    local btnTrack = trackPanel:getChildByName("selected_btn")

    btnRefresh:setVisible(false)
    btnGet:setVisible(false)
    btnReward:setVisible(false)
    trackPanel:setVisible(false)

    if task:getDefaultTotal() > 0 then
        rootBar:setVisible(true)
        local percent = task:getDefaultDone() / task:getDefaultTotal() * 100
        progressBar:setPercent(percent)
        progressText:setString(task:getDefaultDone() .. "/" .. task:getDefaultTotal())
    else
        rootBar:setVisible(false)
    end

    if task:isUnlock() then
        btnGet:setVisible(true)
    elseif task:isTaken() then
        trackPanel:setVisible(true)
    elseif task:isFinished() then
        btnReward:setVisible(true)
        btnReward:setTouchEnabled(true)
    end

    if task:isUnlock() then
        bgBtn:setBright(true)
    else
        bgBtn:setBright(false)
    end

    btnTrack:setBright(not executeFunc(self._onClick, "isNeedTrack", task:getId()))
    local newTaskId = self._onClick("getNewTaskId")
    if newTaskId and newTaskId == tostring(task:getId()) then
        btnTrack:setBright(false) -- 追踪
        self._onClick("setNewTaskId")
    end

    -- 监听事件
    btnRefresh:addClickEventListener(function(sender)
        btnRefresh:setVisible(false)
        -- 暂存位置
        executeFunc(self._onClick, "setRefreshIndex", self._index)

        executeFunc(self._onClick, "REFRESH_BOUNTY_TASK", {taskId = task:getId()})
    end)

    btnReward:addClickEventListener(function(sender)
        if executeFunc(self._onClick, "isBountyTaskFinished") then
            app:popMessage("bountyreachlimit")
            return
        end
        -- 暂存位置
        executeFunc(self._onClick, "setRefreshIndex", self._index)

        executeFunc(self._onClick, "GET_TASK_REWARD", {taskId = task:getId(), taskType = "bounty"})
        btnReward:setTouchEnabled(false)
        -- TaskManager:C2S_GetTaskRewardRequest("bounty", task:getId())
    end)

    btnTrack:addClickEventListener(function(sender)
        local status = not sender:isBright() 
        executeFunc(self._onClick, "TASK_TO_LIST", {add = (not status), taskId = task:getId()})
        -- local taskId = tostring(task:getId())
        -- TaskManager:saveTrackTask(taskId, not status and taskId or "0") 
        sender:setBright(status)
    end)

    btnGet:addClickEventListener(function(sender)
        if executeFunc(self._onClick, "isBountyTaskFinished") then
            app:popMessage("bountyreachlimit")
            return
        end
        -- 暂存新任务ID
        executeFunc(self._onClick, "setNewTaskId", tostring(task:getId()))

        executeFunc(self._onClick, "GET_BOUNTY_TASK", {taskId = task:getId()})
        -- TaskManager:C2S_GetBountyTaskRequest(task:getId())
    end)

    local taskRefresh = executeFunc(self._onClick, "getBountyTaskRefresh", {fameSubId, task:getPos()})
    if taskRefresh and taskRefresh.RefreshNum > 0 and not task:isFinished() then
        btnRefresh:setVisible(true)
    end
end

function ClassRef:onBtnClick(cb)
    self._onClick = cb
end

return ClassRef
